﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using jaysJourney_v01.Common;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using jaysJourney_v01.Utils;
using System.Diagnostics;
using jaysJourney_v01.Utils.Levels;

namespace jaysJourney_v01.States
{
    class LoginState : State
    {
        /**
         * MEMBER VARIABLES
         * */

        // Common

        private SpriteBatch mainSpriteBatch;
        private InputGroup menuButtonGroup;

        private Texture2D interfaceBG;
        private Texture2D loadingscreen;

        // Buttons

        private InputField username;
        private InputField password;
        private Button login;
        private Button register;
        private Button exit;

        bool loading;

        /**
         * METHODS
         * */

        // Constructor

        public LoginState(String name, StateManager stateManager) : base(name, stateManager) { } // Empty

        // init gets called autmatically once

        public override void initialize()
        {
            // State SpriteBatch
            mainSpriteBatch = this.parentSpriteBatch;

            this.interfaceBG = this.getGame().Content.Load<Texture2D>(@"Textures\Interface\main_menu_v5");
            this.loadingscreen = this.getGame().Content.Load<Texture2D>(@"Textures\Preloader\loadingscreen");

            // Buttons
            this.username = new InputField(this, "Enter Username", new Vector2(this.getGame().GraphicsDevice.Viewport.Width / 2 - 150, 350), @"Textures\Textfield\stxtField", @"Textures\Textfield\stxtFieldHover", @"Textures\Textfield\stxtFieldAc", 20, true, false, false);
            this.password = new InputField(this, "Enter Password", new Vector2(this.getGame().GraphicsDevice.Viewport.Width / 2 - 150, 400), @"Textures\Textfield\stxtField", @"Textures\Textfield\stxtFieldHover", @"Textures\Textfield\stxtFieldAc", 20, false, true, false);
            this.login = new Button(this, "LOGIN", @"Textures\Interface\menu_button", @"Textures\Interface\menu_button_ac", 480, Color.White, false, true);
            this.register = new Button(this, "REGISTER", @"Textures\Interface\register", @"Textures\Interface\register_ac", 580, Color.White, false, false);
            this.exit = new Button(this, "EXIT", @"Textures\Interface\quit_btn", @"Textures\Interface\quit_btn_ac", 20, this.getGame().GraphicsDevice.Viewport.Height - 74, Color.White, false, false);

            this.menuButtonGroup = new InputGroup(this, this.mainSpriteBatch);
            this.menuButtonGroup.addButton(username);
            this.menuButtonGroup.addButton(password);
            this.menuButtonGroup.addButton(login);
            this.menuButtonGroup.addButton(register);
            this.menuButtonGroup.addButton(exit);
        }

        public override void update(GameTime gameTime)
        {
            if (loading)
            {
                if (WebLoader.loadUser(this.menuButtonGroup.getContent("Enter Username"), this.menuButtonGroup.getContent("Enter Password"), this.getGame().activePlayer) == 1)
                {
                    if (CONST.VERSION == this.getGame().activePlayer.getVersion())
                    {
                        loading = false;
                        // succeeded loading player
                        this.getGame().pushMessage("Hello, " + this.menuButtonGroup.getContent("Enter Username"), false);
                        this.menuButtonGroup.resetInputField();
                        this.getStateManager().setActiveState("menuState");

                        // check for inc since last login
                        if (this.getGame().activePlayer.getLvlsSinceLast() != 0) this.getGame().pushMessage("Your Levels where played " + this.getGame().activePlayer.getLvlsSinceLast() + " times since your last login and you earned " + this.getGame().activePlayer.getMoneySinceLast() + " money and " + this.getGame().activePlayer.getXpSinceLast() + " xp!", false);
                        WebSaver.creatorResetChanges(this.getGame().activePlayer.getUsername());
                    }
                    else
                    {
                        // Error version control
                        this.getGame().pushMessage("Your version is too old, please get the latest version!", true);
                        this.menuButtonGroup.resetInputField();
                    }
                }
                else if (WebLoader.loadUser(this.menuButtonGroup.getContent("Enter Username"), this.menuButtonGroup.getContent("Enter Password"), this.getGame().activePlayer) == 0)
                {
                    // Error userinput
                    this.getGame().pushMessage("Unknown username or wrong Password!", true);
                    this.menuButtonGroup.resetInputField();
                }
                else
                {
                    // Error database connection
                    this.getGame().pushMessage("Could not connect to the Database!", true);
                    this.menuButtonGroup.resetInputField();
                }
                loading = false;
            }
            // set mouse invis

            // update buttons
            Button click = this.menuButtonGroup.update(gameTime);
            // handle button clicks
            if (click != null && click.getName() == "EXIT") this.getGame().Exit();
            else if (click != null && click.getName() == "LOGIN") {
                loading = true;
            }
            else if (click != null && click.getName() == "REGISTER") this.getStateManager().setActiveState("registerState");
        }

        public override void draw(GameTime gameTime)
        {
            // draw bg
            this.mainSpriteBatch.Draw(this.getGame().menuBg, new Rectangle(0, 0, this.getGame().GraphicsDevice.Viewport.Width, this.getGame().GraphicsDevice.Viewport.Height), new Rectangle(0, 0, this.getGame().menuBg.Width, this.getGame().menuBg.Height), Color.White);

            // draw interface bg
            this.mainSpriteBatch.Draw(this.interfaceBG, new Rectangle((this.getGame().GraphicsDevice.Viewport.Width / 2) - this.interfaceBG.Width / 2, 0, this.interfaceBG.Width, this.interfaceBG.Height), new Rectangle(0, 0, this.interfaceBG.Width, this.interfaceBG.Height), Color.White);

            // draw buttons
            this.menuButtonGroup.draw(gameTime);
            if (loading) this.mainSpriteBatch.Draw(this.loadingscreen, new Rectangle((this.getGame().GraphicsDevice.Viewport.Width / 2) - this.interfaceBG.Width / 2, (this.getGame().GraphicsDevice.Viewport.Height / 2) - this.interfaceBG.Height / 2, this.interfaceBG.Width, this.interfaceBG.Height), new Rectangle(0, 0, this.interfaceBG.Width, this.interfaceBG.Height), Color.White);

            // draw version
            this.mainSpriteBatch.DrawString(this.getGame().textFont, CONST.VERSION, new Vector2(this.getGame().GraphicsDevice.Viewport.Width - 80, this.getGame().GraphicsDevice.Viewport.Height - 40), Color.White);
        }

        public override void onEnterState()
        {
            if (!this.getGame().getGraphDevMan().IsFullScreen)
            {
                this.getGame().getGraphDevMan().PreferredBackBufferWidth = this.getGame().GraphicsDevice.DisplayMode.Width;
                this.getGame().getGraphDevMan().PreferredBackBufferHeight = this.getGame().GraphicsDevice.DisplayMode.Height;
                this.getGame().getGraphDevMan().ToggleFullScreen();
            }

            
            this.username.setNewPosition(350);
            this.password.setNewPosition(400);
            this.login.setNewPosition(480);
            this.register.setNewPosition(580);
            this.exit.setNewPosition(20, this.getGame().GraphicsDevice.Viewport.Height - 74);
        }
    }
}
